package cn.source.system.service.impl;

import cn.source.common.exception.base.BaseException;
import cn.source.system.domain.BuzMember;
import cn.source.system.domain.Vip;
import cn.source.system.domain.VipOrderModel;
import cn.source.system.mapper.VipMapper;
import cn.source.system.service.IBuzMemberService;
import cn.source.system.service.IVipOrderService;
import cn.source.system.service.IVipService;
import cn.source.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tubo
 * @date 2024/06/13
 */
@Service
@Slf4j
public class VipServiceImpl implements IVipService {

    @Resource
    private VipMapper vipMapper;

    @Resource
    private IVipOrderService vipOrderService;

    /**
     * 会员用户表
     */
    @Resource
    private IBuzMemberService buzMemberService;

    @Override
    public int insertVip(Vip vipEntity) {
        return vipMapper.insertVip(vipEntity);
    }

    @Override
    public void confirmVipOrder(Long orderId) {
        VipOrderModel vipOrderModel = new VipOrderModel();
        vipOrderModel.setId(orderId);
        vipOrderModel.setStatus(2);
        vipOrderModel.setPayTime(new Date());
        int num = vipOrderService.update(vipOrderModel);
        if (num > 0) {
            VipOrderModel vipOrderModel1 = vipOrderService.findOneByOrderId(orderId);
            if (vipOrderModel1 == null) {
                throw new BaseException( "vip订单信息为空");
            }

            BuzMember buzMember = buzMemberService.selectBuzMemberById(vipOrderModel1.getOwner());
            if (buzMember == null) {
                log.error("用户不存在:{}", vipOrderModel1.getOwner());
                throw new BaseException("用户不存在1");
            }
            Vip vip = vipMapper.findOne(vipOrderModel1.getVipId());
            if (vip == null) {
                throw new BaseException("vip信息不存在!");
            } else {
                buzMember.setSfvip(1);
                buzMemberService.updateBuzMember(buzMember);
            }
        }
    }

    @Override
    public Vip findOne(Long vipId) {
        return vipMapper.findOne(vipId);
    }
}
